//用来判断是否为红心音乐,默认0为不是,1为是
var checkPre = 0;
//指第几首歌曲
var musicIndex = 1;
//指播放模式
var musicMode = 'list';
var musicId;
var userId;
//指处于哪个电台的状态中
var status = "normal";
var id;
$(function(){
    initPlayer(musicIndex-1);
    audio.volume = 0.8;

    //初始化
    function initPlayer(index){
        //音乐路径
        audio.setAttribute('src',playList[index].path);
        //音乐名字
        $('.song_title').text(playList[index].musicName);
        //音乐歌手
        $('.performer').text(playList[index].singer);
        //cover
        $('.cover').attr('src',playList[index].cover);
        //进度条
        $('.progress').width("0%");
        //传输数据的id
        musicId=playList[index].musicId;
        userId=$('.userId').val();
        musicId
        $.ajax({
            type:'post',
            url:'musicPreference/preferenceMusicExist',
            data:{
                'musicId': musicId,
                'userId' : userId
            },
            success:function(data){
                if(data.result==true){
                    checkPre=1;
                    $(".fi-heart").addClass("red");
                }
                else{
                    $.ajax({
                        type:'post',
                        url:'musicAccumulation/addAccumulationMusic',
                        data:{
                            'musicId': musicId,
                            'userId' : userId
                        },
                        success:function(data){
                            if(data.result==true){
                                checkPre=0;
                                $(".fi-heart").removeClass("red");
                            }
                        }
                    });
                }
            }
        });
        audio.play();
    }

    //播放
    function toPlay(action){
        if (action == 'play') {
            audio.play();
            $('.fi-play').css('display','none');
            $('.fi-pause').css('display','inline');
            $('.cover').css('-webkit-animation-play-state','running');
        }
        else if (action == 'pause') {
            audio.pause();
            $('.fi-pause').css('display','none');
            $('.fi-play').css('display','inline');
            $('.cover').css('-webkit-animation-play-state','paused');
        }
        else if (action == 'next') {
            playMusicMode(action);
        };
    }

    /*进度条的监听事件*/
    audio.addEventListener('timeupdate',function(){
        if (!isNaN(audio.duration)) {
            //播放进度条
            var progressValue = (audio.currentTime/audio.duration)*100;
            $('.progress').width(progressValue+"%");
        };
    },false);

    /*播放结束进入下一首*/
    audio.addEventListener('ended',function(){
        playMusicMode('ended');
    },false);

    //根据播放模式计算歌曲索引
    function playMusicMode(action){
        var musicNum = playList.length;
        var index = musicIndex;
        //列表循环
        if (musicMode == 'list' ) {
            if (action == 'next' || action == 'ended') {
                if (index == musicNum) {//如果是最后一首歌，跳到第一首
                	if(status=='normal'){
                    	index = 1;
                    }
                	if(status=='preBroadcast'){
                        //如果播放的是红心电台
                        $('#preBroadcast').trigger("click");
                    }
                    if(status=='broadcast'){
//                    	alert("!!!!");
                        //如果播放的是公众电台
                    	$.ajax({
                            type: 'post',
                            url: 'broadcast/playPublicBroadcast',
                            data: {
                                'broadcastId' : id
                            },
                            success: function (data) {
                                playList.length = 0;
                                status = 'broadcast';
                                jQuery.each(data, function(i,item){
                                	playList.push( {
                                        'musicId' : item.musicId,
                                        'musicName' : item.musicName,
                                        'singer' :  item.singer,
                                        'cover' : item.cover,
                                        'path' : item.path
                                    });
                                	musicIndex = 1;
                                });
                                playMusicMode("list");
                            }
                        });
                    }
                }
                else{
                    index += 1;
                }
            };
        }
        /*随机播放,现版本不提供
        if (musicMode == 'random') {
            var randomIndex = parseInt(musicNum * Math.random());
            index = randomIndex + 1;
            if (index == musicIndex) {
            //下一首和当前相同，跳到下一首
                index += 1;
            };
        };
        */
        musicIndex = index;
        playIndex(index-1);
    }

    //更新歌曲播放索引，重新加载歌曲，并播放
    function playIndex(index){
        initPlayer(index);
        audio.load();
        toPlay('play');
    }

	/*播放与暂停的切换*/
	$('.fi-pause').mouseenter(function(){
		$('.fi-pause').addClass('shine');
	});
	$('.fi-pause').mouseleave(function(){
		$('.fi-pause').removeClass('shine');
	});
	$('.fi-pause').click(function(){
        toPlay('pause');
	});
	$('.fi-play').click(function(){
        toPlay('play');
	});
	$('.fi-play').mouseenter(function(){
		$('.fi-play').addClass('shine');
	});
	$('.fi-play').mouseleave(function(){
		$('.fi-play').removeClass('shine');
	});
	
	/*红心和垃圾桶*/
	$('.fi-heart').click(function(){
        if(checkPre==0){
		    $.ajax({
			    type:'post',
			    url:'musicPreference/addPreferenceMusic',
			    data:{
                    'musicId': musicId,
                    'userId' : userId,
                    'preference' : 5
                },
			    success:function(data){
                    if(data.result==true){
                        checkPre = 1;
                        $('.fi-heart').addClass('red');
                    }
			    }
	        });
        }
        else {
            $.ajax({
                type: 'post',
                url: 'musicPreference/deletePreferenceMusic',
                data: {
                    'musicId': musicId,
                    'userId': userId
                },
                success: function (data) {
                    if(data.result==true){
                        checkPre = 0;
                        $('.fi-heart').removeClass('red');
                    }
                }
            });
        }
	});

	$('.fi-heart').mouseenter(function(){
		$('.fi-heart').addClass('shine');
	});
	$('.fi-heart').mouseleave(function(){
		$('.fi-heart').removeClass('shine');
	});
	$('.fi-trash').click(function(){
		//$('.fi-trash').toggleClass("red");
        //如果已经添加为红心音乐,需求重新复位(即删除musicPreference)
        if(checkPre==1){
            $.ajax({
                type: 'post',
                url: 'musicPreference/deletePreferenceMusic',
                data: {
                    'musicId': musicId,
                    'userId' : userId
                },
                success: function (data) {
                }
            });
        }
        $.ajax({
            type:'post',
            url:'musicDislike/addDislikeMusic',
            data:{
                'musicId': musicId,
                'userId' : userId
            },
            success:function(data){
            }
        });
        toPlay('next');
	});
	$('.fi-trash').mouseenter(function(){
		$('.fi-trash').addClass('shine');
	});
	$('.fi-trash').mouseleave(function(){
		$('.fi-trash').removeClass('shine');
	});
	
	/*音量*/
	$('.fi-volume').mouseenter(function(){
		$('.fi-volume').addClass('shine');
		$('.volume_wrap').css('display','inline');
	});
	
	$('.volume_wrap').mouseout(function(){
		$('.fi-volume').removeClass('shine');
		$('.volume_wrap').css('display','none');
	});

	$('.volume_bar').slider({
	      value: 80,
	      orientation: "vertical",
	      range: "min",
	      animate: true,
	      change:function(event,ui){   
               selection=$('.volume_bar').slider('value');
               if(selection=='0'){
                  audio.muted = true;
                  $('.fi-volume-strike').removeClass('hidden');
                  $('.fi-volume').addClass('hidden');
               }
               else audio.volume = (selection/80).toFixed(2);
          }  
	});

    $('.fi-volume').click(function(){
        $('.fi-volume-strike').removeClass('hidden');
        $('.fi-volume').addClass('hidden');
        audio.muted = true;
        $('.volume_bar').slider('value','0');
    });

    $('.fi-volume-strike').click(function(){
        $('.fi-volume').removeClass('hidden');
        $('.fi-volume-strike').addClass('hidden');
        audio.muted = false;
        $('.volume_bar').slider('value','60');
    });

    /*切歌*/
	$('.fi-next').mouseenter(function(){
		$('.fi-next').addClass('shine');
	});
	$('.fi-next').mouseleave(function(){
		$('.fi-next').removeClass('shine');
	});
    $('.fi-next').click(function(){
        toPlay('next');
    });
	
	/*注销?为甚shine没用TAT*/
    /*是注销,是因为css把它先填黄色了*/
	$('.fi-power').mouseenter(function(){
		$('.fi-power').addClass('shine');
	});
	$('.fi-power').mouseleave(function(){
		$('.fi-power').removeClass('shine');
	});
	$('.fi-power').click(function(){
        self.location.href = 'user/userExit';
    });

	/*个人主页*/
	$('.tag').click(function(){
		$('.leftContent').animate({width:'toggle'});
	});

    /*个人头像*/
    $('#headPortrait').click(function(){
        window.open('page/user') ;
    });

    /*红心电台*/
    $('#preBroadcast').click(function(){
        $.ajax({
            type: 'post',
            url: 'broadcast/playRedHeartBroadcast',
            data: {
            },
            success: function (data) {
            	if(data.length!=0){
                playList.length = 0;
                status = 'preBroadcast';
                jQuery.each(data, function(i,item){
                    playList.push( {
                        'musicId' : item.musicId,
                        'musicName' : item.musicName,
                        'singer' :  item.singer,
                        'cover' : item.cover,
                        'path' : item.path
                    });
                });
                musicIndex =1;
                playMusicMode("list");
            	}
            	else{
            		alert("收藏的红心音乐为0");
            	}
            }
        });
    });

    //公众电台
    $('.broadcast').click(function(){
        $.ajax({
            type: 'post',
            url: 'broadcast/playPublicBroadcast',
            data: {
                'broadcastId' : id=$(this).attr('value')
            },
            
            success: function (data) {
//            	alert(id);
                playList.length = 0;
                status = 'broadcast';
                jQuery.each(data, function(i,item){
                	playList.push( {
                        'musicId' : item.musicId,
                        'musicName' : item.musicName,
                        'singer' :  item.singer,
                        'cover' : item.cover,
                        'path' : item.path
                    });
                	musicIndex = 1;
                });
                playMusicMode("list");
            }
        });
    });
    
    /*私人电台
    $('#privateBroadcast').click(function(){
    	 $.ajax({
            type: 'post',
            url: 'broadcast/playPrivateBroadcast',
            data: {
               
            },
            success: function (data) {
                playList.length = 0;
                status = 'privateBroadcast';
                jQuery.each(data, function(i,item){
                	playList.push( {
                        'musicId' : item.musicId,
                        'musicName' : item.musicName,
                        'singer' :  item.singer,
                        'cover' : item.cover,
                        'path' : item.path
                    });
                	musicIndex = 1;
                });
                playMusicMode("list");
            }
        });
    });
    */
});
